﻿using System;
using KinonekoSoftware.CodariaDB.Presets;

namespace KinonekoSoftware.Studio
{
#if AVALONIA
 using System.Diagnostics;
    using System.Windows;
    using Avalonia.Media;
    using Avalonia.Media.Imaging;

    public class ImageEditFeedback
    {
        public Bitmap Image        { get; init; }
        public byte[] ImageBuffer  { get; init; }
        public byte[] OriginBuffer { get; init; }
        public string ImageID      { get; init; }
    }

    public partial class Platform
    {
        #region Image Pool

        public static Bitmap GetDefaultImage()
        {
            var    avatar  = GravatarSystem.GetPresetAvatarFilePath(GetRepository().Workspace);
            var    setting = Ioc.Get<JuXiaoYouSetting>();
            byte[] buffer;
            if (setting.Preset_Avatar == Preset_Avatar.Override)
            {
                if (File.Exists(avatar))
                {
                    buffer = File.ReadAllBytes(avatar);
                    return MediaFactory.ToBitmap(buffer);
                }

                setting.Preset_Icon = Preset_Icon.Icon_5;
            }


            buffer = GravatarSystem.GetPresetAvatar(setting.Preset_Avatar);
            return MediaFactory.ToBitmap(buffer);
        }

        public static void CacheInUIThread(string id, byte[] buffer)
        {
            Xplat.RunOnUIThread(() =>
            {
                var ms = new MemoryStream(buffer);
                var bi = new Bitmap(ms);
                Cache.TryAdd(id, new WeakReference<object>(bi));
            });
        }

        #endregion

        public static void OpenLink(string uri, bool useKey = false)
        {
            if (string.IsNullOrEmpty(uri))
            {
                return;
            }

            if (useKey)
            {
                uri = LanguageManager.GetText(uri);
            }

            if (string.IsNullOrEmpty(uri))
            {
                return;
            }


            Process.Start(new ProcessStartInfo
            {
                UseShellExecute = true,
                FileName        = "explorer.exe",
                Arguments       = uri,
            });
        }

    }
#endif
}